package access.spg.android.com.accessibilitytest;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.Intent;
import android.graphics.Path;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import java.util.Random;

@RequiresApi(api = Build.VERSION_CODES.O)
public class SmallVideo2Service extends AccessibilityService {
    private static String tag="SmallVideoService";

    private static boolean start=false;

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(tag,"onServiceConnected");
//        searchNode();
    }

    @Override
    public void onInterrupt() {
        Log.d(tag,"onInterrupt");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(tag,"onUnbind");
        return super.onUnbind(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        switch (event.getEventType())
        {
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:
                scrollUp(false);
                break;
        }
    }






    /**
     * 向上翻页
     *
     *
     * */
    private void scrollUp(boolean loop)
    {
        if(!start)
        {
            start=true;
        }else
        {
            if(!loop)
                return;
        }


        try {//滞留
            Random ra =new Random();
            int tmp;
            tmp=ra.nextInt(10)+50;
            Log.e(tag, "============scrollUp wait "+tmp+"s===============" );
            Thread.sleep(tmp*1000);

            Toast.makeText(this,"时间："+tmp,Toast.LENGTH_SHORT).show();

            GestureDescription.Builder builder = new GestureDescription.Builder();
             Path path = new Path();
             float y1 = MyUtil.getWindowHeight(this)*0.8f;
             float y2 = MyUtil.getWindowHeight(this)*0.1f;
             float x =MyUtil.getWindowWidth(this)/2f;
             path.moveTo(x, y1);
             path.lineTo(x, y2);
             GestureDescription gestureDescription = builder
                .addStroke(new GestureDescription.StrokeDescription(path, 200L, 800L,false))
                .build();

             dispatchGesture(gestureDescription, new GestureResultCallback() {
                 @Override
                 public void onCompleted(GestureDescription gestureDescription) {
                     super.onCompleted(gestureDescription);
                     Log.e(tag, "============Swipe onCompleted===============" );
                 }

                 @Override
                 public void onCancelled(GestureDescription gestureDescription) {
                     super.onCancelled(gestureDescription);
                     Log.e(tag, "============Swipe onCancelled===============");
                 }
             }, new Handler(Looper.getMainLooper()));

            scrollUp(true);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     }







}
